fix: send error to client when SSE stream disconnects without response#1949
Open
skyvanguard wants to merge 4 commits intomodelcontextprotocol:mainfrom
Open
fix: send error to client when SSE stream disconnects without response#1949skyvanguard wants to merge 4 commits intomodelcontextprotocol:mainfrom
skyvanguard wants to merge 4 commits intomodelcontextprotocol:mainfrom
Conversation
added 4 commits
January 24, 2026 09:25
When the SSE stream disconnects before the server sends a response and no event ID has been received (making reconnection impossible), the client was left hanging indefinitely on read_stream.receive(). Now the transport sends a JSONRPCError to unblock the client, which surfaces as an McpError. Also handles the case where reconnection attempts are exhausted without receiving a response. Github-Issue: modelcontextprotocol#1811 Reported-by: ivanbelenky
- Add pragma: no cover on max reconnection attempts guard (requires complex server mock to trigger) - Add pragma: no branch on isinstance check in _send_disconnect_error (always True in practice since only called for JSONRPCRequest) - Add pragma: no cover on test functions that run inside multiprocessing.Process (coverage can't measure subprocess code) Github-Issue:modelcontextprotocol#1811
The SSE stream disconnect test now exercises this code path, making the pragma: no cover annotation invalid per strict-no-cover. Github-Issue:modelcontextprotocol#1811
The pytest.raises context manager always expects the exception; the fall-through branch (no exception) is never taken. Github-Issue:modelcontextprotocol#1811
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #1811
When the SSE stream disconnects before the server sends a response (e.g., due to a read timeout on long-running tool calls), and no event ID has been received (making reconnection impossible), the client was left hanging indefinitely on
read_stream.receive(). Thepost_writertask had already returned from_handle_sse_response()without sending anything toread_stream_writer, leaving the session layer permanently blocked.Changes
Added
_send_disconnect_error()method toStreamableHTTPTransportthat sends aJSONRPCError(code-32000) to theread_stream_writer, unblocking the client. This is called in two scenarios:_handle_sse_response(): When the SSE stream ends without receiving a response andlast_event_idisNone(no event ID to reconnect with)_handle_reconnection(): When maximum reconnection attempts are exhausted without receiving a responseThe error surfaces as an
McpErrorat the session layer, allowing the caller to handle the failure gracefully instead of hanging forever.Root Cause
The issue was introduced when reconnection support was added. The reconnection logic correctly handles the case where
last_event_id is not None(reconnect using Last-Event-ID), but theelsebranch (no event ID available) was missing, causing a silent return that left the client blocked.Test plan
Added 2 regression tests in
tests/issues/test_1811_sse_disconnect_hang.py:test_client_receives_error_on_sse_disconnect: Server has a 30s tool, client has 0.5s read timeout. Verifies the client receivesMcpErrorwith "SSE stream disconnected" message instead of hanging.test_fast_tool_still_works_normally: Ensures fast tool calls continue to work correctly with the fix in place.